package com.yt.product.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import com.yt.product.models.bo.SupplierInfoBO;
import com.yt.product.models.bo.SupplierInfoSaveBO;
import com.yt.product.models.dto.SupplierInfoDTO;
import com.yt.product.models.service.ISupplierInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * 供应商信息控制器
 *
 * @author lianyadong
 * @Date 2025-09-03 14:16:03
 */
@RestController
@RequestMapping("/cms/supplierInfo")
@Slf4j
@Api(value = "SupplierInfoController", tags = "供应商信息接口")
public class SupplierInfoController {

    @DubboReference
    private ISupplierInfoService supplierInfoService;

    @ApiOperation(value = "添加供应商信息", notes = "添加供应商信息")
    @PostMapping("/insert")
    public Result insert(@RequestBody SupplierInfoSaveBO saveBO) {
        boolean flag = supplierInfoService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存供应商信息", notes = "保存供应商信息")
    public Result save(@RequestBody SupplierInfoSaveBO saveBO) {
        if (saveBO.getId() == null) {
            // 添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @ApiOperation(value = "修改供应商信息", notes = "修改供应商信息")
    @PostMapping("/update")
    public Result update(@RequestBody SupplierInfoSaveBO saveBO) {
        boolean flag = supplierInfoService.update(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    // 修改状态接口重构
    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态", notes = "修改状态")
    public Result updateStatus(@Valid @RequestBody SupplierInfoBO request) {
        boolean flag = supplierInfoService.updateStatus(request.getId(), request.getStatus());
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    // 删除接口重构
    @PostMapping("/delete")
    @ApiOperation(value = "删除供应商信息", notes = "删除供应商信息")
    public Result delete(@Valid @RequestBody SupplierInfoBO request) {
        boolean flag = supplierInfoService.deleteById(request.getId());
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除供应商信息", notes = "批量删除供应商信息")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        boolean success = supplierInfoService.deleteBatchIds(Arrays.asList(arr));
        return success ? new Result<>() : new Result<>(CodeEnum.FAIL_100006);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<SupplierInfoDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        SupplierInfoDTO dto = supplierInfoService.selectById(id);
        return new Result<>(dto);
    }

    @ApiOperation(value = "供应商信息列表", notes = "供应商信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SupplierInfoDTO>> queryPageList(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) SupplierInfoBO bo) {
        // 1. 构建查询条件
        EntityWrapper<SupplierInfoDTO> wrapper = new EntityWrapper<>();
        if (bo != null) {
            // 添加对供应商名称精确查询的支持
            if (bo.getSupplierName() != null && !bo.getSupplierName().trim().isEmpty()) {
                wrapper.eq("supplier_name", bo.getSupplierName().trim());
            }
            if (bo.getStatus() != null) {
                wrapper.eq("status", bo.getStatus());
            }
            if (bo.getStartDate() != null) {
                wrapper.ge("create_time", bo.getStartDate());
            }
            if (bo.getEndDate() != null) {
                wrapper.le("create_time", bo.getEndDate());
            }
            // 如果有keyword，则进行模糊查询
            if (bo.getKeyword() != null && !bo.getKeyword().trim().isEmpty()) {
                wrapper.like("supplier_name", bo.getKeyword().trim());
            }
        }
        // 默认不查询已删除的供应商（状态≠2），避免前端列表显示已删除数据
        wrapper.ne("status", 2);
        // 2. 分页查询
        Page<SupplierInfoDTO> page = new Page<>(pageNo, pageSize);
        page = supplierInfoService.selectPage(page, wrapper);
        return new Result<>(page);
    }
}
